#version 130

in vec4 Vertex;
in vec2 TexCoord0;

out vec2 PaperPosition;
out vec2 TexCoord;
out vec3 Normal;
out vec4 ProjectedPos;
out vec3 LightDirection;
out vec3 EyeDirection;
//out vec4 MVPosition;

uniform mat4 ModelView;
uniform mat4 Projection;
uniform float TimeElapsed;
uniform vec3 LightPosition;
uniform vec3 EyePosition;
uniform sampler2D TexState;
uniform sampler2D TexNoise;

uniform float DeformFactor;
void main(void)
{
  PaperPosition = Vertex.xz;
  TexCoord = gl_MultiTexCoord0.st;
  Normal = gl_Normal;
  
  const float noiseRatio = 0.9;
  const float noiseMultiplier = 2;
  float noise = (1.0-noiseRatio) + 
    noiseRatio * float(texture2D(TexNoise, TexCoord));
  noise *= noiseMultiplier;
  float state = float(texture2D(TexState, TexCoord)) 
	/ DeformFactor * noise;
  
  vec4 MVPosition = ModelView * (Vertex - vec4(Normal,0) * state);
  gl_Position = Projection * MVPosition;

  ProjectedPos = gl_Position;
  LightDirection = LightPosition - MVPosition.xyz;
  EyeDirection = EyePosition - MVPosition.xyz;
}
